package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.PopupWindowAnimation;
import com.movecar.utils.RequestResultCallback;
import com.movecar.utils.imageloader.Constants;
import com.movecar.widget.CustomProgressDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 主页沾光活动
 * 
 * @author Zhao.JQ
 *
 */
public class MainAlertActivity extends Activity implements OnClickListener {
	private TextView title_text;
	private ImageView group_img;
	private TextView content_text;
	private String couponTitle;
	private String logo;
	private String content;
	private String rbMsgId;
	private String shareId;
	private ImageLoader imageLoader = null;
	private CustomProgressDialog progressDialog = null;
	private CloudBeansBean mCloudBeansBean;
	private View mainView;
	private LayoutInflater layoutInflater;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);mainView = layoutInflater.inflate(R.layout.main_joined_alert, null);
		setContentView(mainView);
		couponTitle = getIntent().getStringExtra("couponTitle");
		logo = getIntent().getStringExtra("logo");
		content = getIntent().getStringExtra("content");
		shareId = getIntent().getStringExtra("shareId");
		rbMsgId = getIntent().getStringExtra("rbMsgId");
		initView();
	}

	private void initView() {
		imageLoader = ImageLoader.getInstance();
		title_text = (TextView) findViewById(R.id.title_text);
		content_text = (TextView) findViewById(R.id.content_text);
		group_img = (ImageView) findViewById(R.id.group_img);

		Button ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);

		imageLoader.displayImage(AppConfig.HOST + logo, group_img, Constants.image_display_options,
				AppConfig.animateListener);
		title_text.setText(couponTitle);
		content_text.setText(content);
	}

	/**
	 * 抢红包
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void getRedBag() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "getRedBagNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("shareId", shareId));
		params.add(new BasicNameValuePair("rbMsgId", rbMsgId));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if ("0".equals(mCloudBeansBean.getResultCode())) {
					if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
						new PopupWindowAnimation(mainView, MainAlertActivity.this);
					}
					Alert.info(MainAlertActivity.this, "抢红包成功！");
					setResult(RESULT_OK);
					finish();
				} else {
					stopProgressDialog();
					Alert.error(MainAlertActivity.this, result);
				}
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(MainAlertActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.ok:
			startProgressDialog();
			getRedBag();

			break;
		default:
			break;
		}

	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
}
